<template>
  <div class="left-menu">
    <div class="menu-title">会员中心</div>
    <a
        v-for="item in menuItems"
        :key="item.url"
        :href="item.url"
        :class="['menu-item', { active: item.active }]"
        @click.prevent="selectMenu(item)">
      {{ item.name }}
    </a>
  </div>
</template>

<script setup>
import {ref, onMounted} from 'vue'
import {useRouter} from 'vue-router'

const router = useRouter()

const menuItems = ref([
  {name: '个人信息', url: '/member/info', active: false},
  {name: '重置密码', url: '/member/modify_password', active: false},
  {name: '购物车', url: '/member/shopping_cart', active: false},
  {name: '我的收藏', url: '/member/my_collect', active: false},
  {name: '我的订单', url: '/member/my_order', active: false},
  {name: '我的地址', url: '/member/my_address', active: false}
])

const selectMenu = (selectedItem) => {
  menuItems.value.forEach(item => {
    item.active = item.url === selectedItem.url
  });
  router.push(selectedItem.url);
}
onMounted(()=>{
  menuItems.value.forEach(item => {
    item.active = item.url === router.currentRoute.value.path
  })
});
</script>
<style scoped>
/* 左侧菜单样式 */
.left-menu {
  width: 200px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 15px 0;
  margin-right: 20px;
  margin-left: 20px;
}

.menu-title {
  padding: 10px 20px;
  font-size: 16px;
  font-weight: bold;
  color: #333;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 10px;
}

.menu-item {
  padding: 10px 20px;
  display: block;
  color: #666;
  text-decoration: none;
  transition: all 0.3s;
}

.menu-item:hover, .menu-item.active {
  background-color: #f0f7ff;
  color: #007BFF;
  border-left: 3px solid #007BFF;
}

</style>